# install
# 1. pip install PySpice
# 2. pyspice-post-installation --install-ngspice-dll
# 3. pyspice-post-installation --check-install


snippet spice_template "PySpice template"
import PySpice.Logging.Logging as Logging
logger = Logging.setup_logging()

from PySpice.Spice.Netlist import Circuit
from PySpice.Unit import *

circuit = Circuit('Resistor Bridge')
circuit.V('input', 1, circuit.gnd, u_V(10))
circuit.R(1, 1, 2, u_kΩ(2))
circuit.R(2, 1, 3, u_kΩ(1))
circuit.R(3, 3, 2, u_kΩ(2))
circuit.R(4, 3, circuit.gnd, u_kΩ(2))
circuit.R(5, 2, circuit.gnd, u_kΩ(1))
# print("The Circuit/Netlist:\n{0}".format(circuit))

simulator = circuit.simulator(temperature=25, nominal_temperature=25)
analysis = simulator.operating_point()
# print("The Simulator:\n{0}".format(simulator))

for node in analysis.nodes.values():
    print('Node {}: {:4.1f} V'.format(str(node), float(node)))
endsnippet

snippet spice_raw "pass raw spice definitions to a netlist"
import PySpice.Logging.Logging as Logging
logger = Logging.setup_logging()

from PySpice.Spice.Netlist import Circuit
from PySpice.Unit import *

circuit = Circuit('Test')

circuit.raw_spice = '''
R1  3   0   30
R2  3   1   10
R3  3   2   30
R4  2   1   10
R5  2   0   30

V1  1   0   120V
'''

simulator = circuit.simulator(temperature=25, nominal_temperature=25)
analysis = simulator.operating_point()

for node in analysis.nodes.values():
    print('Node {}: {:4.3f} V'.format(str(node), float(node)))
endsnippet
